@extends('admin.app')

@section('breadcrumb')
    <div class="bg-light lter b-b wrapper-md">
        <ol class="breadcrumb bg-light lter m-n no-padder">
            <li><a href="{{ admin_action('HomeController@index') }}">主页</a></li>
            <li>业务管理</li>
            <li class="active">用户管理</li>
        </ol>
    </div>
@endsection

@section('content')
    <div class="panel panel-default">
        <div class="panel-heading font-bold">
        销售统计
        </div>
        <div class="row wrapper">
        	<!-- 1 start --><div class="col-sm-10">
                <form class="form-horizontal" method="get"
                      action="{{admin_action('Business\SalesStatisticsController@salesStatistics')}}">
                    <div class="row">
                        <div class="col-sm-10">
                            <div class="form-group">
                        <div class="col-sm-6">
                            <div class="form-group">
                                <label class="control-label col-sm-4">客户:</label>
                                <div class="col-sm-8">
                                    <input class="form-control" type="text" placeholder="姓名 用户名 手机 邮箱" name="username"
                                           value="{{ $query['username'] }}"/>
                                </div>
                            </div>
                        </div>
                        <div class="col-sm-6">
                            <div class="form-group">
                                <label class="control-label col-sm-4">机构:</label>
                                <div class="col-sm-8">
                        <select class="form-control org_yxjg" name="org_code" data-dept-url="{{ admin_action('Business\UsersController@orgDept') }}" data-rule-required="true">
                            <option value="">
                                请选择营销机构
                            </option>
                            @foreach($all_org as $org)
                                <option value="{{ $org->code }}" {{ $query['org_code'] == $org->code?'selected=selected':'' }}>{{ $org->company_name }}({{ $org->code }})</option>
                            @endforeach
                        </select>
                                </div>
                            </div>
                        </div>
                        <div class="col-sm-6">
                            <div class="form-group">
                                <label class="control-label col-sm-4">部门:</label>
                                <div class="col-sm-8">
                        <select class="form-control org_dept" data-rule-required='true' name='dept_code'>
                            <option value="">
                                请选择机构下属部门
                            </option>
                        </select>
                                </div>
                            </div>
                        </div>
                        <div class="col-sm-6">
                            <div class="form-group">
                                <label class="control-label col-sm-4">经纪人:</label>
                                <div class="col-sm-8">
                                <input type="text" class="form-control" name="broker_name"
                                           value="{{ $query['broker_name'] }}" placeholder="姓名">
                                </div>
                            </div>
                        </div>
                        <div class="col-sm-6">
                            <div class="form-group">
                                <label class="control-label col-sm-4">存量时间点:</label>
                                <div class="col-sm-8">
                                    
                                    <input type="text" class="form-control datetimepicker" name="cl_time"
                                           value="{{ $query['cl_time'] }}" placeholder="默认当前时间">
                                </div>
                            </div>
                        </div>
                        <div class="col-sm-11">
                                <label class="control-label col-sm-2">时间节点:</label>
                                <div class="col-sm-5">
                                    
                                    <input type="text" class="form-control datetimepicker" name="start_time"
                                           value="{{ $query['start_time'] }}" placeholder="默认当天">

                                </div>
                                <div class="col-sm-5">
                                    
                                    <input type="text" class="form-control datetimepicker" name="end_time"
                                           value="{{ $query['end_time'] }}" placeholder="默认当天">

                                </div>
                        </div>
                            </div>
                        </div>
                <!-- <div class="line line-dashed b-b line-lg"></div> -->
                        <div class="col-sm-1 text-left">
            <span class="input-group-btn">
              <button class="btn btn-sm btn-default" type="submit">查询</button>
            </span>
                        </div>
                    </div>
                </form>
            </div>
            <div class="col-sm-2 text-right text-center-xs">

                <a class="btn btn-xs btn-primary"
                       href="{{ admin_action('Business\OrganizationalManagerController@orgList') }}"
                       data-toggle="tooltip" title="机构销售统计">
                    <i class="fa fa-tasks"></i>
                </a>
                <a class="btn btn-xs btn-primary"
                       href="{{ admin_action('Business\OrganizationalManagerController@deptList') }}"
                       data-toggle="tooltip" title="部门销售统计">
                    <i class="fa fa-sitemap"></i>
                </a>
                  <a class="btn btn-xs btn-primary"
                       href="{{ admin_action('Business\OrganizationalManagerController@brokerList') }}"
                       data-toggle="tooltip" title="经纪人销售统计">
                    <i class="fa fa-group"></i>
                </a>
        <a href="{{ admin_action('Business\SalesStatisticsController@salesStatistics',['download'=>'download']) }}" class="btn btn-sm btn-primary btn-addon">
                        <i class="fa fa-download"></i>excel导出查询结果
                    </a>
            </div>
            <!-- 1 end -->
        </div>
        <div class="table-responsive">

           <table class="table table-striped b-t b-light">
                <thead>
                    <th>用户总数</th>
                    <th>到期</th>
                    <th>交易</th>
                    <th>未封包交易(当前)</th>
                    <th>未还款交易(当前)</th>
                    <th>流失</th>
                    <th>续投</th>
                    <th>新增</th>
                    <th>时点存量</th>
                <tbody>
                    <tr>
                        <td>{{ $tj->user_count }}人</td>
                        <td>{{ $tj->hk_sum }}元&nbsp;&nbsp;{{ $tj->hk_count }}人</td>
                        <td>{{ $tj->tz_sum }}元&nbsp;&nbsp;{{ $tj->tz_count }}人</td>
                        <td>{{ $tj->wfbcl_sum }}元&nbsp;&nbsp;{{ $tj->wfbcl_count }}人</td>
                        <td>{{ $tj->whkcl_sum }}元&nbsp;&nbsp;{{ $tj->whkcl_count }}人</td>
                        <td>{{ $tj->ls_sum }}元&nbsp;&nbsp;{{ $tj->ls_count }}人</td>
                        <td>{{ $tj->xt_sum }}元&nbsp;&nbsp;{{ $tj->xt_count }}人</td>
                        <td>{{ $tj->xz_sum }}元&nbsp;&nbsp;{{ $tj->xz_count }}人</td>
                        <td>{{  floor($tj->sdcl_sum }})元&nbsp;&nbsp;{{ $tj->sdcl_count }}人</td>
                    </tr>
                </tbody>
           </table>
           <br/>
           <table class="table table-striped b-t b-light">
                <thead>
                <tr>
                    <th>用户id</th>
                    <th>姓名</th>
                    <th>用户名</th>
                    <th>经纪人</th>
                    <th>二级经纪人</th>
                    <th>手机号码</th>
                    <th>到期</th>
                    <th>交易</th>
                    <th>未封包交易(当前)</th>
                    <th>未还款交易(当前)</th>
                    <th>流失</th>
                    <th>续投</th>
                    <th>新增</th>
                    <th>时点存量</th>
                    <th>投资笔数</th>
                    <th>操作</th>
                </tr>
                </thead>
                <tbody>
                @forelse ($data as $d)
                    <tr>
                    	<td>{{ $d->user_id }}</td>
                    	<td>
                            @if(empty($d->name))
                                --
                            @else
                                <a href="/admin/business/sales_statistics/user_list/{{ $d->user_id }}" data-toggle="tooltip" title="客户详情">{{ $d->name??'--' }}</a>
                            @endif
                        <td>
                            @if(empty($d->name))
                                --
                            @else
                                <a href="/admin/business/sales_statistics/user_list/{{ $d->user_id }}" data-toggle="tooltip" title="客户详情">{{ $d->username??'--' }}</a>
                            @endif
                            </td>
                    	<td>
                        @if(empty($d->broker_name))
                            --
                        @else
                            <a href="/admin/business/sales_statistics/broker_list/{{ $d->broker_id }}" data-toggle="tooltip" title="经纪人详情">{{ $d->broker_name }}</a>
                        @endif
                        </td>
                        <td>
                            {{ $d->two_level_broker_name or '--' }}
                        </td>
                    	<td>
            @if(!empty($d->phone_number))
              {{substr($d->phone_number, 0, 3) . '******' . substr($d->phone_number,7) }}
            @else
              --
            @endif</td>
                    	<td>{{ $d->hk }}</td>
                        <td>{{ $d->tz }}</td>
                        <td>
                            {{
                                $d->wfbcl
                            }}
                        </td><td>
                            {{
                                $d->whkcl
                            }}
                        </td>
                        <td>
                            @if( $d->hk > $d->tz )
                                {{ $d->hk - $d->tz }}
                            @else
                                0
                            @endif
                        </td>
                    	<td>
                    		@if( $d->hk >= $d->tz )
                                {{ $d->tz }}
                            @else
                                {{ $d->hk }}
                    		@endif
                    	</td>
                    	<td>
                    		@if( $d->hk < $d->tz )
                    			{{ $d->tz - $d->hk }}
                    		@else
                    			0
                    		@endif
                    	</td>
                    	<td>
                    		{{
                    			$d->sdfbcl
                    		}}
                    	</td>
                    	<td><?php $query['username']= $d->name ?><a 
                       href="{{ admin_action('Business\SalesStatisticsController@index',$query) }}"
                       data-toggle="tooltip" title="投资记录">{{
                                $d->tzbs
                            }}
                </a>    
                    		
                    	</td>                        <!-- 瑞信营销 -->
                        @if($admin_user->hasRole('RXYX') && $d->org_code == $rxyx_code)
                                    <td>
                                        <a class="btn btn-xs btn-primary" href="{{ admin_action('Business\SalesStatisticsController@userInfo',['id'=>$d->user_id]) }}"
                                   data-toggle="tooltip" title="用户详情">
                                <i class="fa fa-info"></i>
                            </a>
                            <a class="btn btn-xs btn-primary" href="{{ admin_action('Business\SalesStatisticsController@transactionRecord',['id'=>$d->user_id]) }}"
                                   data-toggle="tooltip" title="交易流水">
                                <i class="fa fa-align-left"></i>
                            </a>
                            <a class="btn btn-xs btn-primary"
                                   href="{{ admin_action('Business\SalesStatisticsController@userToAgent',['user_id'=>$d->user_id]) }}"
                                   data-toggle="tooltip" title="提升为经纪人">
                                <i class="fa fa-arrow-up"></i>
                            </a>
                                    </td>
                        @else
                            <td>
                                <label class="text-info">待开发</label>
                            </td>
                        @endif
                    </tr>
                @empty
                    <tr>
                        <td colspan="13" class="text-center">数据未找到 ......</td>
                    </tr>
                @endforelse
                </tbody>
            </table>

        </div>
        <div class="panel-footer">
            <div class="row">
                <div class="col-sm-12 text-right text-center-xs">
                	{!! $links !!}
                </div>
            </div>
        </div>
    </div>
@endsection
@section('scripts')
    <script type="text/javascript">
        var dept_code = '{{ $query['dept_code'] }}';
        $(function(){
            //动态查询营销机构的部门
            $('.org_yxjg').change(function(){
                $dept_url = $(this).data('dept-url');
                $org_code = $(this).find('option:selected').val();
                $('.org_dept').html("");
                //异步查询营销机构下级部门
                $.ajax({
                    url:$dept_url,
                    data:{org_code:$org_code},
                    success:function(data){
                        node = '';
                        if(data.length > 0){
                            for(var i in data){
                                if(dept_code === data[i]["code"])
                                    node = node+'<option selected=selected value ='+data[i]["code"]+'>'+data[i]["name"]+'</option>';
                                else
                                    node = node+'<option value ='+data[i]["code"]+'>'+data[i]["name"]+'</option>';
                            }
                            $('.org_dept').append($(node));
                        }else{
                            node = node+'<option value ='+""+'>'+"  --  "+'</option>';
                            $('.org_dept').append($(node));
                        }
                    }
                });
            });
            $('.org_yxjg').trigger("change");
        });
    </script>
@endsection